package com.xb;

import java.util.LinkedList;

/**
 * @program: register-jerry
 * @description: 生产者-消费者模型，采用LinkedList实现，内存大小是100
 * 采用 wait notifyAll 组合使用，采用线程通信
 * 顺带理一下 synchronized moniter 计数器，以及wait notify  wait set集合
 * sleep和wait的区别
 * @author: xu.bin
 * @create: 2020-11-08
 */
public class QueueDemo {

    private static Integer MAX_VALUE = 100;
    private static LinkedList<String> queue = new LinkedList<>();

    public synchronized void push(String e) throws InterruptedException {
        while (queue.size() == MAX_VALUE) {
            wait();
        }
        queue.add(e);
        notifyAll();
    }

    public synchronized String pop() throws InterruptedException {
        while (queue.size() == 0) {
            wait();
        }
        String e = queue.removeLast();
        notifyAll();
        return e;
    }
}